﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using LeagueOfRila.Model;


namespace LeagueOfRila.Controller
{
    public class MenuCharacterChoiceController : BaseController
    {
        private KeyboardState previousState;

        public override string viewName
        {
            get { return "LeagueOfRila.View.MenuCharacterChoiceView"; }
            set { }
        }
        public override string modelName
        {
            get { return "LeagueOfRila.Model.MenuCharacterChoiceModel"; }
            set { }
        }
        public MenuCharacterChoiceController()
        {
        }

        public MenuCharacterChoiceModel GetModel()
        {
            return this.model as MenuCharacterChoiceModel;
        }


        public override void Update(GameTime gameTime)
        {
            KeyboardState currentState = Keyboard.GetState();

            if (previousState == null)
                previousState = currentState;

            base.Update(gameTime);
            //if (Keyboard.GetState().IsKeyDown(Keys.A))
            //    this.GetModel().pseudo += "A";

            List<Keys> listprevious = previousState.GetPressedKeys().ToList();
            if (this.GetModel().lockMenu == false)
            {
                foreach (Keys key in listprevious)
                {
                    if (!currentState.GetPressedKeys().Contains(key))
                    { 
                        if (key.ToString().ToLower() == "q")
                        {
                            Client.PlaySound(Client.keysound);
                            if (this.GetModel().GetHoverLeft() == false)
                                this.GetModel().displayHoverLeft();
                            if (this.GetModel().selectedCharacter == 0)
                                this.GetModel().selectedCharacter = this.GetModel().listCharacter.Count() - 1;
                            else
                                this.GetModel().selectedCharacter = this.GetModel().selectedCharacter - 1;
                        }
                        else if (key.ToString().ToLower() == "d")
                        {
                            Client.PlaySound(Client.keysound);
                            if (this.GetModel().GetHoverRight() == false)
                                this.GetModel().displayHoverRight();
                            if (this.GetModel().selectedCharacter == this.GetModel().listCharacter.Count() - 1)
                                this.GetModel().selectedCharacter = 0;
                            else
                                this.GetModel().selectedCharacter = this.GetModel().selectedCharacter + 1;
                        }
                        else if (key.ToString().ToLower() == "enter")
                        {
                            // appel server HF
                           
                            Client.player.character = this.GetModel().listCharacter[this.GetModel().selectedCharacter];
                            this.GetModel().PlayerReady(Client.player.pseudo, Client.player.character.name);
                            
                            this.GetModel().lockMenu = true;

                            //son personnage choisi
                            switch (Client.player.character.name)
                            {
                                case "Priest":
                                    Client.PlaySound(Client.priestReady);
                                    break;

                                case "War":
                                    Client.PlaySound(Client.warReady);
                                    break;

                                case "Mage":
                                    Client.PlaySound(Client.mageReady);
                                    break;

                                case "Matthieu":
                                    Client.PlaySound(Client.rogueReady);
                                    break;

                            }
                           

                        }
                    }
                }
            }
            
            previousState = currentState;

        }

        public void startGame()
        {
            Client.GotoNextStep("Game");
        }
    }
}
